<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class SaleModel extends Model
{	


    // 获取销售单库存信息
    function getdetailStoreInfo($pro_id,$prochar_id,$warehouse_id,$prochar_qty,$lang)
    {
        if($pro_id && $prochar_id){
            $res['store_sum'] = getValue('store',['prochar_id'=>$prochar_id,'pro_id'=>$pro_id,'warehouse_id'=>$warehouse_id],'qty');
            $res['store_ctn'] = $prochar_qty ? intval($res['store_sum'])/intval($prochar_qty) :0;
            $info = getSearch('area_type','zu',['zu.id area_type_id,mu.content area_type_name,a.id area_id,a.name'],['mu.lang'=>$lang,'zu.id'=>['in','2,3'],'a.warehouse_id'=>$this->getAdminWarehouse(session('userid'))],[['multi_lang mu','zu.mkey = mu.mkey','LEFT'],['warehouse_area a','zu.id = a.type_id']],'zu.id desc');

            $res['totalStore'] = $res['store_sum'];

            if(!empty($info)){
                foreach ($info as $k => $v) {
                    $info[$k]['qty'] = getValue('store2area',['pro_id' => $pro_id,'prochar_id' => $prochar_id,'area_id'=>$v['area_id'],'warehouse_id'=>$this->getAdminWarehouse(session('userid'))],'qty');
                }
            }
            $res['info'] = $info;
            return $res;
        }
    }


    

    function getPickup($sale_id,$pro_id,$prochar_id,$prochar_qty)
    {   
        $res = getValue('make_pickup_detail',['sale_id'=>$sale_id,'pro_id'=>$pro_id,'prochar_id'=>$prochar_id],'qty');
        if($res){
            return $res;
        }else{
            return 0;
        }
    }
    function getProcess($sale_id,$pro_id,$prochar_id,$prochar_qty)
    {   
        $res = getValue('make_process_detail',['sale_id'=>$sale_id,'pro_id'=>$pro_id,'prochar_id'=>$prochar_id],'qty');
        if($res){
            return $res;
        }else{
            return 0;
        }
    }

    function getStoreOrder($sale_id,$pro_id,$prochar_id,$prochar_qty)
    {   
        $res = getValue('store_order_detail',['sale_id'=>$sale_id,'pro_id'=>$pro_id,'prochar_id'=>$prochar_id],'qty');
        if($res){
            return $res;
        }else{
            return 0;
        }
    }

	// 获取区域
    function getWarehouseArea($type=true,$where=[],$field=['*'])
    {
    	if($type){ 
    		$return = getDatas('warehouse_area',$field,$where);
    	}else{
    		$return = getValue('warehouse_area',$where,$field);
    	}
        return $return;
    }


    //  获取admin_id  绑定的仓库ID
    function getAdminWarehouse($admin_id)
    {
        return getValue('warehouse2admin',['admin_id'=>$admin_id],'warehouse_id');
    }

    // 获取包装工名单
    function getTeam_names($team_ids)
    {
        $team = getDatas('admin',['nickname'],['id'=>['in',$team_ids]]);
        $team_names = implode( array_column($team, 'nickname'),' / ' );
        return $team_names;
    }

    function getDockerAOrPacker($table,$where,$field,$table_)
    {
        $id = getValue($table,$where,$field);
        $res = $id ? getValue('admin',['id'=>getValue($table_,['id'=>$id],'create_id')],'nickname') : '-';
        return $res;
    }

    // 获取目的地
    function getArea_name($target,$area_id)
    {
        $target_name = $target == 0 ? '销售订单' : $target == 1 ? '散货' : $target == 2 ? '包装部' : '';
        return $target_name.getValue('warehouse_area',['id'=>$area_id],'name'); 
    }

    function saleMissingList($data,$lang,$warehouse_id)
    {

        // 判断当前缺货登记的产品是否已经被仓管员登记过了。
        $where['check_id']     = 0;
        $where['pro_id']       = $data['pro_id'];
        $where['prochar_id']   = $data['prochar_id'];
        $where['warehouse_id'] = $warehouse_id;
        $where_['sale_id'] = $data['sale_id'];
        $missingData = Db::name('store_missing')
                            ->where('id', 'IN', function ($query) use ($where_) {
                                $query->name('store_missing_detail')->where($where_)->field('missing_id');
                            })
                            ->where($where)
                            ->find();
        $sale_ids = '';
        if($missingData){
            $sale_ids =  array_column( getDatas('store_missing_detail',['sale_id'],['missing_id'=>$missingData['id']]),'sale_id');
        }
        // dump($missingData);die;

        $return = [];
        if( isset($data['pro_id']) && isset($data['prochar_id']) ){

            $field = ['s.priority,s.saler_id,s.create_at,s.create_id,zu.qty,zu.pro_id,zu.prochar_id,zu.sale_id'];
            $join = [['sale s','zu.sale_id = s.id']];
            // 获取登记订单
            $res = getSearch('sale_detail','zu',$field,['zu.pro_id'=>$data['pro_id'],'zu.prochar_id'=>$data['prochar_id'],'zu.sale_id'=>$data['sale_id']],$join,['zu.id desc']);
            // 获取受影响订单
            $return = getSearch('sale_detail','zu',$field,['zu.pro_id'=>$data['pro_id'],'zu.prochar_id'=>$data['prochar_id'],'s.create_at'=>['gt',$res[0]['create_at']]],$join,['zu.id desc']);
            $return[] = $res[0];
            $qtySum = 0;
            if(!empty($return)){
                foreach ($return as $k => $v) {
                    $return[$k]['saler_name'] = getValue('admin',['id'=>$v['saler_id']],'nickname');
                    $return[$k]['create_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
                    $return[$k]['create_time'] = getTimeDates($v['create_at'],$lang);
                    $return[$k]['detail'] = getDatas('sale_detail',['*'],['sale_id'=>$v['sale_id']]);
                    if(!empty($return[$k]['detail'])){
                        foreach ($return[$k]['detail'] as $key => $value) {
                            $return[$k]['detail'][$key]['proInfo'] = getProAndProchar($value,$lang,48);
                        }
                    }
                    $qtySum += $v['qty'];
                }
            }

        }
        $returnData['list'] = $return;
        $returnData['sale_ids'] = $sale_ids;
        $returnData['qtySum'] = $qtySum;
        return $returnData;
    }

    // 分页获取数据
    function getPagination($data,$pageSize,$page)
    {
        $i = 0;
        $arr = [];
        foreach ($data['list'] as $k1 =>$v1){
            if($i == $pageSize*$page) break;
            $arr[$k1] = $v1;
            unset($arr[$k1]['detail']);
            foreach ($v1['detail'] as $key => $value) {
                if($i < $pageSize*$page && $i >= ($page-1)*$pageSize){
                    if($i == $pageSize*$page) break;
                    $arr[$k1]['detail'][] = $value;
                }
                $i++;
            }
        }
        return $arr;
    }


    // 获取支付方式
    function getSalePpayType()
    {
        $SalePpayType = getSearch('sale_pay_type','zu',['zu.id,zu.limit,mu.content,f.fail'],['mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey'],['co_sale_fail f','zu.id = f.pay_type_id','LEFT']],'zu.id desc');
        return $SalePpayType;
    }


    function addSaleLog($sale_id,$option,$remark)
    {
        $has = hasData('sale_log',['sale_id'=>$sale_id,'option'=>$option,'remark'=>$remark]);
        if(!$has){
            $res = insertData('sale_log',['sale_id'=>$sale_id,'option'=>$option,'remark'=>$remark,'create_id'=>session('admin')['id'],'create_at'=>time()]);
        }
        return true;
    }
    
}